package top.fangwenmagician.calculationstring.operator;


import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <h1>正则运算符抽象类</h1>
 * 该类是正则运算符抽象类的基类<br />
 */
abstract class A_RegularOperator implements I_Operator {
    /**
     * 运算符匹配的正则表达式
     *
     * @return 正则表达式
     */
    public abstract String regularText() throws Exception;

    @Override
    public int peerPriority(String expression) throws Exception {
        Matcher matcher = Pattern.compile(this.regularText()).matcher(expression);
        if (matcher.find()) {
            return expression.length() - matcher.start();
        } else {
            return -1;
        }
    }

    @Override
    public boolean isExist(String expression) throws Exception {
        return Pattern.compile(this.regularText()).matcher(expression).find();
    }
}
